package client;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import tools.Card;
import tools.Player;

import javax.swing.*;

public class PlayerPanel extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8580385201641871121L;
	private ClientBadugi client;
	private ArrayList<JLabel> cardImage = new ArrayList<JLabel>();
	private ArrayList<JCheckBox> checkCard = new ArrayList<JCheckBox>();
	private int playerID;
	private JLabel playerIDLabel;
	private JLabel playerFunction;
	private JLabel playerStateLabel;
	private JLabel inGameChips;
	private JLabel chips;
	private Font fontPlain;
	
	PlayerPanel(ClientBadugi client, int playerID)
	{
		this.client = client;
		this.playerID = playerID;
		initialize();
		setOffCheckCard();
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize()
	{
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		playerIDLabel = new JLabel("Player ID: "+ this.playerID);
		GridBagConstraints gbc_playerIDLabel = new GridBagConstraints();
		gbc_playerIDLabel.insets = new Insets(4, 4, 4, 4);
		gbc_playerIDLabel.gridwidth = 2;
		gbc_playerIDLabel.gridx = 0;
		gbc_playerIDLabel.gridy = 0;
		gbc_playerIDLabel.anchor = GridBagConstraints.WEST;
		add(playerIDLabel, gbc_playerIDLabel);
		playerIDLabel.setForeground(Color.WHITE);
		fontPlain = playerIDLabel.getFont();
		
		playerFunction = new JLabel("Dealer button");
		GridBagConstraints gbc_playerFunction = new GridBagConstraints();
		gbc_playerFunction.insets = new Insets(4, 4, 4, 4);
		gbc_playerFunction.gridwidth = 2;
		gbc_playerFunction.gridx = 2;
		gbc_playerFunction.gridy = 0;
		gbc_playerFunction.anchor = GridBagConstraints.EAST;
		add(playerFunction, gbc_playerFunction);
		playerFunction.setForeground(Color.YELLOW);
		
		playerStateLabel = new JLabel("Wymienil: 4 karty");
		GridBagConstraints gbc_playerStateLabel = new GridBagConstraints();
		gbc_playerStateLabel.insets = new Insets(4, 4, 4, 4);
		gbc_playerStateLabel.gridwidth = 4;
		gbc_playerStateLabel.gridx = 0;
		gbc_playerStateLabel.gridy = 1;
		gbc_playerStateLabel.anchor = GridBagConstraints.CENTER;
		add(playerStateLabel, gbc_playerStateLabel);
		playerStateLabel.setForeground(Color.WHITE);
		
		checkCard.add(0,new JCheckBox());
		GridBagConstraints gbc_checkCard1 = new GridBagConstraints();
		gbc_checkCard1.insets = new Insets(4, 4, 4, 4);
		gbc_checkCard1.gridx = 0;
		gbc_checkCard1.gridy = 2;
		checkCard.get(0).setBackground(new Color(0, 100, 0));
		add(checkCard.get(0), gbc_checkCard1);
		
		checkCard.add(1, new JCheckBox());
		GridBagConstraints gbc_checkCard2 = new GridBagConstraints();
		gbc_checkCard2.insets = new Insets(4, 4, 4, 4);
		gbc_checkCard2.gridx = 1;
		gbc_checkCard2.gridy = 2;
		checkCard.get(1).setBackground(new Color(0, 100, 0));
		add(checkCard.get(1), gbc_checkCard2);
		
		checkCard.add(2, new JCheckBox());
		GridBagConstraints gbc_checkCard3 = new GridBagConstraints();
		gbc_checkCard3.insets = new Insets(4, 4, 4, 4);
		gbc_checkCard3.gridx = 2;
		gbc_checkCard3.gridy = 2;
		checkCard.get(2).setBackground(new Color(0, 100, 0));
		add(checkCard.get(2), gbc_checkCard3);
		
		checkCard.add(3, new JCheckBox());
		GridBagConstraints gbc_checkCard4 = new GridBagConstraints();
		gbc_checkCard4.insets = new Insets(4, 4, 4, 4);
		gbc_checkCard4.gridx = 3;
		gbc_checkCard4.gridy = 2;
		checkCard.get(3).setBackground(new Color(0, 100, 0));
		add(checkCard.get(3), gbc_checkCard4);
		
		cardImage.add(0, new JLabel());
		GridBagConstraints gbc_cardImage1 = new GridBagConstraints();
		gbc_cardImage1.insets = new Insets(4, 4, 4, 4);
		gbc_cardImage1.gridx = 0;
		gbc_cardImage1.gridy = 3;
		add(cardImage.get(0), gbc_cardImage1);
		
		cardImage.add(1, new JLabel());
		GridBagConstraints gbc_cardImage2 = new GridBagConstraints();
		gbc_cardImage2.insets = new Insets(4, 4, 4, 4);
		gbc_cardImage2.gridx = 1;
		gbc_cardImage2.gridy = 3;
		add(cardImage.get(1), gbc_cardImage2);
		
		cardImage.add(2, new JLabel());
		GridBagConstraints gbc_cardImage3 = new GridBagConstraints();
		gbc_cardImage3.insets = new Insets(4, 4, 4, 4);
		gbc_cardImage3.gridx = 2;
		gbc_cardImage3.gridy = 3;
		add(cardImage.get(2), gbc_cardImage3);
		
		cardImage.add(3, new JLabel());
		GridBagConstraints gbc_cardImage4 = new GridBagConstraints();
		gbc_cardImage4.insets = new Insets(4, 4, 4, 4);
		gbc_cardImage4.gridx = 3;
		gbc_cardImage4.gridy = 3;
		add(cardImage.get(3), gbc_cardImage4);
		
		inGameChips = new JLabel("Bid: 10000");
		GridBagConstraints gbc_inGameChips = new GridBagConstraints();
		gbc_inGameChips.insets = new Insets(4, 4, 4, 4);
		gbc_inGameChips.gridwidth = 2;
		gbc_inGameChips.gridx = 0;
		gbc_inGameChips.gridy = 4;
		gbc_inGameChips.anchor = GridBagConstraints.WEST;
		add(inGameChips, gbc_inGameChips);
		inGameChips.setForeground(Color.WHITE);
		
		chips = new JLabel("Chips: 100000");
		GridBagConstraints gbc_chips = new GridBagConstraints();
		gbc_chips.insets = new Insets(4, 4, 4, 4);
		gbc_chips.gridwidth = 2;
		gbc_chips.gridx = 2;
		gbc_chips.gridy = 4;
		gbc_chips.anchor = GridBagConstraints.WEST;
		add(chips, gbc_chips);
		chips.setForeground(Color.WHITE);
		
		setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.white));
		setBackground(new Color(0, 100, 0));
		this.setForeground(Color.WHITE);
		
		initPanel();
	}
	
	private void setOffCheckCard()
	{
		for(JCheckBox ck : checkCard)
			ck.setVisible(false);
	}

	void uncheckCards()
	{
		for(JCheckBox ck : checkCard)
			ck.setSelected(false);
	}
	
	private void setOnCheckCard()
	{
		for(JCheckBox ck : checkCard)
			ck.setVisible(true);
	}
	
	ArrayList<Card> getCheckedCard() // pobranie wszystkich zaznaczoych kart
	{
		int j=0;
		Player p = this.client.clientControl.gameState.getPlayer(this.playerID);
		ArrayList<Card> cards = new ArrayList<Card>();
		for(int i=0; i<checkCard.size(); i++)
		{
			if (checkCard.get(i).isSelected())
				cards.add(j++, p.getHand().getCardList().get(i));
		}
		return cards;
	}

	void initPanel()
	{
		playerIDLabel.setFont(fontPlain);		
		playerIDLabel.setText("Player ID: "+ this.playerID);
		setOffCheckCard();
		uncheckCards();
		for(JLabel l : cardImage)
			l.setIcon(client.cardImages.getIcon("no_card"));
		playerFunction.setText("");
		playerStateLabel.setText("");
		inGameChips.setText("Bid:");
		chips.setText("Chips:");
		setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.WHITE));
	}
	
	private boolean isMyPanel()
	{
		return (this.client.clientControl.id == this.playerID);  
	}
	
	boolean isPanelUnused()
	{
		return (this.playerID >= this.client.clientControl.gameState.players.size());
	}
	
	boolean isTurningPlayer()
	{
		return (this.client.clientControl.gameState.getTurningPlayer() == this.playerID);
	}
	
	private boolean isEndGame()
	{
		return this.client.clientControl.gameState.isEndGame();
	}
	
	void refreshPanel()
	{
		if (!this.client.isConnection())
		{
			initPanel();
			return;
		}
		
		if (isPanelUnused())
		{
			initPanel();
			playerStateLabel.setText("Slot unused");
			return;	
		}
		
		Player p = this.client.clientControl.gameState.getPlayer(this.playerID);
	
		if (p.isEmptySlot())
		{
			initPanel();
			playerStateLabel.setText("Empty Slot");
			setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.WHITE));
			return;
		}
		
		if (isTurningPlayer())
		{
			setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.RED));
		}
		else
		{
			setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.WHITE));
		}
		
		if (isMyPanel())
		{
			playerIDLabel.setFont( new Font(playerIDLabel.getFont().getFontName(), Font.BOLD, playerIDLabel.getFont().getSize()));
			if(p.isDealerButton())
				playerIDLabel.setText("You (DB)");
			else
				playerIDLabel.setText("You");
		}
		else
		{
			playerIDLabel.setFont( new Font(playerIDLabel.getFont().getFontName(), Font.PLAIN, playerIDLabel.getFont().getSize()));
			if(p.isDealerButton())
				playerIDLabel.setText("Player ID: "+ this.playerID + " (DB)");
			else
				playerIDLabel.setText("Player ID: "+ this.playerID);
		}
		/* pokazanie swoich kart/ kart przeciwnikow na koncu gry */
		if (isMyPanel() && p.isPlaying() || (!isMyPanel() && p.isPlaying() && isEndGame()))
		{
			if (isMyPanel()) 
				setOnCheckCard();
			else
				setOffCheckCard();
			
			if (p.getHand() != null)
			{
				/* ustawienie obrazkow kart dla swojego gracza */
				for(int i=0; i<4; i++)
					cardImage.get(i).setIcon(client.cardImages.getIcon(p.getHand().getCardList().get(i).getRankSuit()));
			}
		}
		else if (!isMyPanel() && p.isPlaying())
		{		
			setOffCheckCard();
			for(JLabel l : cardImage)
				l.setIcon(client.cardImages.getIcon("face_down"));			
		}	
		else
		{
			setOffCheckCard();
			for(JLabel l : cardImage)
				l.setIcon(client.cardImages.getIcon("no_card"));
		}
		
		if (p.isBigBlind())
			playerFunction.setText("Big Blind");
		else if (p.isSmallBlind())
			playerFunction.setText("Small Blind");
		else
			playerFunction.setText("");
 
		if (p.isWaiting())
		{
			playerStateLabel.setText("Waiting for game");
			inGameChips.setText("Bid:");
			chips.setText("Chips: " + p.getTotalChips());
		}
		else
		{
			playerStateLabel.setText(p.getStatusGame());
			inGameChips.setText("Bid: " + p.getInGameChips());
			chips.setText("Chips: " + p.getTotalChips());
		}
	}
}
